package com.passwd.designcore.utils;

public class KeyUtils {
    public static final byte[] K1={0x01,0x23,0x45,0x67,(byte) 0x89,(byte) 0xab,(byte) 0xcd,(byte) 0xef,(byte) 0xfe,(byte) 0xdc,(byte) 0xba,(byte) 0x98,0x76,0x54,0x32,0x10};
    public static final byte[] K2={0x11,0x32,0x08,0x42,(byte) 0x12,(byte) 0xed,(byte) 0xbe,(byte) 0xad,(byte) 0xac,(byte) 0xba,(byte) 0xdd,(byte) 0x24,0x23,0x65,0x46,0x33};
    public static final byte[] S1={0x65,0x18,0x45,0x07,(byte) 0x78,(byte) 0xea,(byte) 0xed,(byte) 0xec};
    public static final String outputEncryptFileName="out_encrypt.txt";
    public static final String outputDecryptFileName="out_decrypt.txt";

    public static final String inputEncryptFileName="in_encrypt.txt";
    public static final String inputDecryptFileName="in_decrypt.txt";

    public static byte[] fillTo128Old(String key) throws Exception {
        byte[] inputByte=key.getBytes();
      //  return fillTo128(inputByte);
        return fillZeroTo128(inputByte);
    }

    public static byte[] fillZeroTo128(String key) throws Exception {
        byte[] inputByte=key.getBytes();
        //  return fillTo128(inputByte);
        return fillZeroTo128(inputByte);
    }
    public static byte[] fillZeroTo128(byte[] inputByte){
        byte[] bytes = new byte[16];
        for (int i = 0; i < bytes.length; i++) {
            if(i<inputByte.length){
                bytes[i]=inputByte[i];
            }
            else{
                bytes[i]=0;
            }
        }
        return bytes;
    }
    /**
     * java实现SM4 PKCS7Padding填充模式
     * 原理：与16的倍数进行相比，缺少多少位就填充多少位的位数值
     * 例：test字节数为4，填充12个0x0c；12345678字节数为8，填充8个8
     * @param inputByte
     * @return
     * @throws Exception
     */
    public static byte[] fillTo128Old(byte[] inputByte) throws Exception {

        //判断参数是否为空,为空抛出错误
        if (inputByte.length==0) {
            throw new Exception("数据异常，PKCS5Padding填充模式错误，字节参数为空！");
        }
        try {
            // 获字节长度
            int length = inputByte.length;
            // 补齐位数
            int leftLength = 16 - (length % 16 == 0 ? 16 : length % 16);
            // 定义新字节
            byte[] arrayReturn = new byte[length + leftLength];
            // 定义填充字节
            byte[] plusbyte = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
                    0x0f };
            // 是否满足为16字节倍数
            if (leftLength > 0) {
                // 不满足16倍数自动填充
                for (int i = 0; i < length + leftLength; i++) {
                    if (i < length) {
                        // 赋值
                        arrayReturn[i] = inputByte[i];
                    } else {
                        // 补齐位数
                        arrayReturn[i] = plusbyte[leftLength];
                    }
                }
                // System.out.println("填充的字节："+plusbyte[leftLength]);
            } else {
                // 为16字节倍数不需要补齐，直接返回
                return inputByte;
            }
            return arrayReturn;
        } catch (Exception e) {
            throw new Exception("数据异常，PKCS5Padding填充模式错误，异常抛出！" + e.getMessage());
        }
    }
}
